﻿using ActiveStoryTouch.DataModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml.Serialization;
using System.IO;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace TestSuite.DataModel
{
    
    
    /// <summary>
    ///This is a test class for GestureManagerTest and is intended
    ///to contain all GestureManagerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GestureManagerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GestureManager.LoadFromFile
        ///</summary>
        [TestMethod()]
        public void GestureManagerLoadFromFileTest()
        {
            GestureManager_Accessor target = new GestureManager_Accessor();
            GestureManager gestureManager = (GestureManager)target.Target;
            gestureManager.Add(CommonTestConstants.TestGestureIdentifier, new Gesture { Identifier = CommonTestConstants.TestGestureIdentifier });
            gestureManager.Add("tap", new Gesture { Identifier = "tap", DisplayName = "Tap" });
            string fileName = CommonTestConstants.GestureListFileName;
            target.SaveToFile(fileName);
            gestureManager.Clear();

            gestureManager = GestureManager.LoadFromFile(fileName);
            Assert.IsNotNull(gestureManager);
            Assert.AreEqual<int>(2, gestureManager.Count);
            Assert.AreEqual<bool>(true, gestureManager.ContainsKey("tap"));
            Assert.AreEqual<String>(CommonTestConstants.TestGestureIdentifier, gestureManager[CommonTestConstants.TestGestureIdentifier].Identifier);
        }

         /// <summary>
        ///A test for KeyList
        ///</summary>
        [TestMethod()]
        public void KeyListTest()
        {
            GestureManager target = new GestureManager();
            target.Add(CommonTestConstants.TestGestureIdentifier, new Gesture { Identifier = CommonTestConstants.TestGestureIdentifier });
            target.Add("tap", new Gesture { Identifier = "tap" });

            List<String> expected = new List<String> { CommonTestConstants.TestGestureIdentifier, "tap" };

            List<string> actual;
            actual = target.KeyList;
            Assert.AreEqual<int>(expected.Count, actual.Count);
            Assert.AreEqual<bool>(true, actual.Contains(expected[0]));
            Assert.AreEqual<bool>(true, actual.Contains(expected[1]));
        }

        /// <summary>
        ///A test for ObjectAtIndex
        ///</summary>
        [TestMethod()]
        public void ObjectAtIndexTest()
        {
            GestureManager target = new GestureManager();
            target.Add(CommonTestConstants.TestGestureIdentifier, new Gesture { Identifier = CommonTestConstants.TestGestureIdentifier });
            Gesture tapGesture = new Gesture { Identifier = "tap" };
            target.Add("tap", tapGesture);

            int index = 1;
            Gesture expected = tapGesture;
            Gesture actual;
            actual = target.ObjectAtIndex(index);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for SaveToFile
        ///</summary>
        [TestMethod()]
        [DeploymentItem("ActiveStoryTouch.DataModel.dll")]
        public void SaveToFileTest()
        {
            GestureManager_Accessor target = new GestureManager_Accessor();
            GestureManager gestureManager = (GestureManager)target.Target;
            gestureManager.Add(CommonTestConstants.TestGestureIdentifier, new Gesture { Identifier = CommonTestConstants.TestGestureIdentifier });
            gestureManager.Add("tap", new Gesture { Identifier = "tap", DisplayName = "Tap" });
            string fileName = CommonTestConstants.GestureListFileName; 
            target.SaveToFile(fileName);

            DataContractSerializer serializer = new DataContractSerializer(typeof(GestureManager));
            FileStream fileStream = new FileStream(CommonTestConstants.GestureListFileName, FileMode.Open);
            GestureManager result = (GestureManager) serializer.ReadObject(fileStream);
            fileStream.Close();
            Assert.IsNotNull(result);
            Assert.AreEqual<int>(2, result.Count);
            Assert.AreEqual<String>("tap", result["tap"].Identifier);
        }

        /// <summary>
        ///A test for Serializing this class
        ///</summary>
        [TestMethod()]
        public void DataContractSerializationTest()
        {
            GestureManager target = new GestureManager();
            target.Add(CommonTestConstants.TestGestureIdentifier, new Gesture { Identifier = CommonTestConstants.TestGestureIdentifier });
            Gesture tapGesture = new Gesture { Identifier = "tap" };
            target.Add("tap", tapGesture);

            DataContractSerializer serializer = new DataContractSerializer(typeof(GestureManager));
            FileStream fileStream = new FileStream(CommonTestConstants.GestureManagerDataContractTestFileName, FileMode.Create);
            serializer.WriteObject(fileStream, target);
            fileStream.Flush();
            fileStream.Close();

            fileStream = new FileStream(CommonTestConstants.GestureManagerDataContractTestFileName, FileMode.Open);
            GestureManager gestureManagerFromFile = (GestureManager) serializer.ReadObject(fileStream);
            fileStream.Close();

            Assert.AreEqual<int>(target.Count, gestureManagerFromFile.Count);
            Assert.AreEqual<String>(target[CommonTestConstants.TestGestureIdentifier].Identifier, gestureManagerFromFile[CommonTestConstants.TestGestureIdentifier].Identifier);

        }
    }
}
